﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraManager :Singleton<CameraManager> {
    [Tooltip("玩目标位置，玩家")]
    private Transform target;
    [Tooltip("速度因子")]
    public float vK = 1;
    [SerializeField]
    private Vector3 velocity;
    private Transform cmTransform;//缓存自己的transform。提高速度。

    public Transform Target
    {
        get
        {
            return target;
        }

        set
        {
            target = value;
        }
    }

    public override void FixedAwake()
    {
        cmTransform = transform;
    }

    //private float distance;


    private void Start()
    {
        target = GameManager.Instance.Sui.transform;//在start里获取其他组件在Awake里获取的变量
        
    }

    private void Update()
    {
        if (target!=null)//为null时不移动镜头
        {
            velocity = target.position -cmTransform.position;//位置向量差
            float x = velocity.x * Time.deltaTime * vK;
            float y = velocity.y * Time.deltaTime * vK;

            velocity = new Vector3(x, y, 0);//Z轴不移动，赋0
            //distance = velocity.sqrMagnitude;
        
            cmTransform.position += velocity * vK;
            if(Input.GetKey(KeyCode.Escape))
            {
                UIshower.Instance.StartPlay(Nodeal.Quit);
                Time.timeScale = 0;
            }
        }
    }
}
